package com.kurumi.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.kurumi.mapper.MedicalRecordImageMapper;
import com.kurumi.mapper.MedicalRecordPaginationMapper;
import com.kurumi.pojo.MedicalRecordImage;
import com.kurumi.query.MedicalRecordPaginationQuery;
import com.kurumi.service.MedicalRecordPaginationService;
import com.kurumi.util.GuidUtil;

@Service
public class MedicalRecordPaginationServiceImpl implements MedicalRecordPaginationService {
	
	@Autowired
	private MedicalRecordPaginationMapper medicalRecordPaginationMapper;
	
	@Autowired
	private MedicalRecordImageMapper medicalRecordImageMapper;

	@Transactional(readOnly = true)
	@Override
	public List<Map<String, Object>> getUnPaginationImagesByVisitGuid(String visitGuid) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> images = medicalRecordPaginationMapper.getUnPaginationImagesByVisitGuid(visitGuid);
		for (Map<String, Object> image : images) {
			String fileHash = (String)image.get("file_hash");
			String localPath = GuidUtil.getRemoteUrl(fileHash);
			image.put("file_url", localPath);
		}
		return images;
	}

	@Transactional(readOnly = true)
	@Override
	public List<Map<String, Object>> getFinishPaginationImagesByVisitGuid(String visitGuid) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> images = medicalRecordPaginationMapper.getFinishPaginationImagesByVisitGuid(visitGuid);
		for (Map<String, Object> image : images) {
			String fileHash = (String)image.get("file_hash");
			String localPath = GuidUtil.getRemoteUrl(fileHash);
			image.put("file_url", localPath);
		}
		return images;
	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int imagePagination(Integer scanImageId,String newPageTypeCode,String newPageTypeName) {
		// TODO Auto-generated method stub
		MedicalRecordImage medicalRecordImage =medicalRecordImageMapper.selectByPrimaryKey(scanImageId);
		
		if(medicalRecordImage == null){
			throw new RuntimeException(String.format("medicalRecordImage id=(%s)不存在", scanImageId+""));
		}
		Subject subject=SecurityUtils.getSubject();
		Session session = subject.getSession();
		Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
		String userCode =(String)currentUser.get("user_code");
		String userName = (String)currentUser.get("user_name");
		medicalRecordImage.setMrPageTypeCode(newPageTypeCode);
		medicalRecordImage.setMrPageTypeName(newPageTypeName);
		medicalRecordImage.setFirsPigeonholedDateTime(new Date());
		medicalRecordImage.setLastUserId(userCode);
		medicalRecordImage.setLastUserName(userName);
		medicalRecordImage.setLastDateTime(new Date());
		return medicalRecordImageMapper.imagePagination(medicalRecordImage);
	}

	@Transactional(readOnly = true)
	@Override
	public Map<String, Integer> getPaginationCountInfo(String visitGuid) {
		// TODO Auto-generated method stub
		Map<String, Integer> paginationCountInfo = new HashMap<String, Integer>();
		int unPaginationCount = medicalRecordPaginationMapper.getUnPaginationCountByVisitGuid(visitGuid);
		int totalPaginationCount = medicalRecordPaginationMapper.getTotalPaginationCountByVisitGuid(visitGuid);
		paginationCountInfo.put("unPaginationCount", unPaginationCount);
		paginationCountInfo.put("totalPaginationCount", totalPaginationCount);
		return paginationCountInfo;
	}

	@Transactional(readOnly = true)
	@Override
	public List<Map<String, Object>> getPaginationInfoByVisitGuid(String visitGuid) {
		// TODO Auto-generated method stub
		return medicalRecordPaginationMapper.getPaginationInfoByVisitGuid(visitGuid);
	}

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int updatePaginationImageSortIndex(Integer scanImageId, Integer sortIndex) {
		// TODO Auto-generated method stub
		return medicalRecordImageMapper.updateSortIndexById(scanImageId, sortIndex);
	}

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int removePaginationImage(Integer scanImageId) {
		// TODO Auto-generated method stub
		return medicalRecordImageMapper.deleteByPrimaryKey(scanImageId);
	}

	@Transactional(readOnly = true)
	@Override
	public List<Map<String, Object>> getMedicalRecordOfPagination(MedicalRecordPaginationQuery query) {
		// TODO Auto-generated method stub
		return medicalRecordPaginationMapper.getMedicalRecordOfPagination(query);
	}

	@Transactional(readOnly = true)
	@Override
	public int getMedicalRecordCountOfPagination(MedicalRecordPaginationQuery query) {
		// TODO Auto-generated method stub
		return medicalRecordPaginationMapper.getMedicalRecordCountOfPagination(query);
	}

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int batchImagePagination(Integer[] scanImageIds, String newPageTypeCode,String newPageTypeName) {
		// TODO Auto-generated method stub
		Subject subject=SecurityUtils.getSubject();
		Session session = subject.getSession();
		Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
		String userCode =(String)currentUser.get("user_code");
		String userName = (String)currentUser.get("user_name");
		for (Integer scanImageId : scanImageIds) {
			MedicalRecordImage medicalRecordImage =medicalRecordImageMapper.selectByPrimaryKey(scanImageId);
			
			if(medicalRecordImage == null){
				throw new RuntimeException(String.format("medicalRecordImage id=(%s)不存在", scanImageId+""));
			}
			
			medicalRecordImage.setMrPageTypeCode(newPageTypeCode);
			medicalRecordImage.setMrPageTypeName(newPageTypeName);
			medicalRecordImage.setFirsPigeonholedDateTime(new Date());
			medicalRecordImage.setLastUserId(userCode);
			medicalRecordImage.setLastUserName(userName);
			medicalRecordImage.setLastDateTime(new Date());
			medicalRecordImageMapper.imagePagination(medicalRecordImage);
		}
		return 1;
	}

}
